//
//  ATVInstanceController.m
//  FlashController
//
//  Created by Martin Schiøth Dyrby on 11/15/11.
//  Copyright 2011 Molamil. All rights reserved.
//

#import "ATVInstanceController.h"

@implementation ATVInstanceController
@synthesize instanceService;
@synthesize editorView;
@synthesize instanceView;
@synthesize javaScriptController;


- (id)init
{
    self = [super init];
    if (self) {
        // Initialization code here.
        
        [[NSNotificationCenter defaultCenter] 
         addObserver:self 
         selector: @selector(loadNewURLHandler:) 
         name:@"loadNewURL" 
         object: nil];
    }
    
    return self;
}

-(void) loadNewURLHandler: (NSNotification *) note {
    [instanceService cleanAll];
    [editorView updateEditorView:[instanceService instances]];
}


/* JAVASCRIPT INTEGRATION */

- (void)addTypeDef:(NSString *) message {
    [instanceService addInstanceFromXMLString: message];
    [editorView updateEditorView:[instanceService instances]];
}

- (void)removeTypeDef:(NSString *) path {
    [instanceService removeInstanceWithPath: path];
    [editorView updateEditorView:[instanceService instances]]; 
    NSDictionary *dict = [[NSDictionary alloc] initWithObjectsAndKeys:path, @"path", nil];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"removePath" object:dict];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"clearWindow" object:nil];
}

- (void) asEditProp: (NSString*) propName forType: (NSString*) type {
    
}

- (void) asValue: (NSString*) value forPath: (NSString*) path {

    NSDictionary* dict = [[NSDictionary alloc] initWithObjectsAndKeys:value, @"value", path, @"path", nil];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"updateValue" object:dict];
}

- (void) asObjectId: (NSString*) value {
    [javaScriptController setSwfName: value];
}

+ (NSString *) webScriptNameForSelector:(SEL)sel
{
    
	NSString* name;
    if (sel == @selector(addTypeDef:))
		name = @"addTypeDef";
    
    if (sel == @selector(removeTypeDef:))
		name = @"removeTypeDef";
	
	if(sel == @selector(asValue:forPath:))
		name = @"asValueforPath";
	
	if(sel == @selector(asObjectId:))
		name = @"asObjectId";
    
    if(sel == @selector(asEditProp:forType:))
        name = @"asEditPropForType";
    
    return name;
}

+ (BOOL)isSelectorExcludedFromWebScript:(SEL)sel
{
    if (sel == @selector(addTypeDef:)) return NO;
    if (sel == @selector(removeTypeDef:)) return NO;
    if (sel == @selector(asValue:forPath:)) return NO;
    if (sel == @selector(asObjectId:)) return NO;
    if (sel == @selector(asEditProp:forType:)) return NO;
    
    return YES;
}





/* ACTIONS */

- (IBAction)onEditorClick:(id)sender {
    
    ATVInstance *instance = [instanceService getInstanceAtIndex: [sender selectedRow]];
    
    [instanceView updateInstanceView:instance];
    
}








/* CUSTOM GETTERS / SETTERS */
- (void) setBrowserView:(ATVBrowserView *)newBrowserView {
    [newBrowserView retain];
    [browserView release];
    browserView = newBrowserView;
    
    [browserView setFrameLoadDelegate: self];
}

- (ATVBrowserView *) browserView {
    return browserView;
}




/* WEBVIEW FRAME DELEGATE */
- (void)webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame 
{
	[[browserView windowScriptObject] setValue:self forKey:@"flashController"];
}



@end
